<cfcomponent>

	<cffunction name="init" output="false" access="public" returntype="SaveRecord" hint="I perform initialization for this component">
		<cfargument name="KalendarDAO" type="any" required="true" hint="I am the Kalendar Data Abstraction"/>
		<cfargument name="CategoryService" type="any" required="true" hint="I am the Kalendar Category Data abstraction"/>
		<cfargument name="Validator" type="any" required="true" hint="I perform validation functions for this component">
	
			<cfset variables.instance.KalendarDAO = arguments.KalendarDAO />
			<cfset variables.instance.CategoryService = arguments.CategoryService />
			<cfset variables.instance.Validator = arguments.Validator />
		
		<cfreturn this />	
	</cffunction>
	
	<cffunction name="execute" output="false" access="public" returntype="boolean" hint="I execute this command">
		<cfargument name="KalendarEvent" type="any" required="true"/>
		<cfargument name="UserMsg" type="any" required="true" >
		<cfset var returnState = false />
		
		<!--- <cftransaction action="begin"> --->
		<!--- validate the passed arguments --->
		<cfif getValidator().validate( argumentcollection=arguments ) >
	
			<!--- See if we need to add a category --->
			<cfif arguments.KalendarEvent.hasNewCategory() >
				<cfset arguments.KalendarEvent.setCategoryID( getCategoryService().saveNewCategory( arguments.KalendarEvent.getNewCategoryName() ) )>
				<!--- This function uses ducktyping. The KalendarEvent cfc functions as a CategoryBean --->
				<cfif Len(Trim( arguments.KalendarEvent.getCategoryID() )) >
					<cfset arguments.UserMsg.addSuccess("Category Saved!") />
				<cfelse>
					<!--- Ok, that didn't work. Make a note of it and just get out of this function. --->
					<cfset arguments.UserMsg.addError("Category Save Failed!") />
					<cfreturn false />
				</cfif>
			</cfif>
			
			<!--- Try to add the record --->
			<cfif getKalendarDAO().save( arguments.KalendarEvent ) >
				<cfset arguments.UserMsg.addSuccess("Event Saved!") />
			<cfelse>
				<!--- Adding the record didnt' work. Make a note of it and just get out of this function. --->
				<cfset arguments.UserMsg.addError("Event Save Failed!") />
				<cfreturn false />
			</cfif>
		<cfelse>
			<cfreturn false />
		</cfif>

		<!--- </cftransaction> --->
		
		<cfset returnState = true >

		<cfreturn returnState />
		
	</cffunction>
	
	<cffunction name="getKalendarDAO" access="private" output="false" returntype="any">
		<cfreturn variables.instance.KalendarDAO />
	</cffunction>
	
	<cffunction name="getCategoryService" access="private" output="false" returntype="any">
		<cfreturn variables.instance.CategoryService />
	</cffunction>
	
	<!--- This for the composed validator --->
	<cffunction name="getValidator" access="private" output="false" returntype="any">
		<cfreturn variables.instance.Validator />
	</cffunction>
	
</cfcomponent>